﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMSModel;

public partial class Topics_Topics_step1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (Request["flag"] == "edit")
            {
                CMSEntities cms = new CMSEntities();
                int tid = int.Parse(Request["tid"]);
                var c = cms.Topics2.First(x => x.Id == tid);
                txtSummary.Text = c.Summary;
                txtTitle.Text = c.Title;
                hidImageUrl.Value = c.ImageUrl;
                hidImageUrl1.Value = c.ImageUrl2;
                hidImageUrl3.Value = c.ImageUrl3;
                Image1.ImageUrl = "~/file/topics/face/" + c.ImageUrl;
                Image2.ImageUrl = "~/file/topics/face/" + c.ImageUrl2;
                Image3.ImageUrl="~/file/topics/face/" + c.ImageUrl3;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        
        CMSEntities cms = new CMSEntities();
        CMSModel.Topics2 t = new CMSModel.Topics2();
        if (Request["flag"] == "edit")
        {
            int tid = int.Parse(Request["tid"]);
            t = cms.Topics2.First(x => x.Id == tid);
        }
        t.Summary = txtSummary.Text;
        t.Title = txtTitle.Text;
        t.ImageUrl = hidImageUrl.Value;
        t.ImageUrl2 = hidImageUrl1.Value;
        t.ImageUrl3 = hidImageUrl3.Value;
        if (Request["flag"] != "edit")
        {
            t.CTime = DateTime.Now;
            cms.Topics2.AddObject(t);
        }
        cms.SaveChanges();
        Response.Redirect("Topics_step2.aspx?tid=" + t.Id); 
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string path=Server.MapPath("~/file/topics/face/");
        if(!System.IO.Directory.Exists(path))
        {
            System.IO.Directory.CreateDirectory(path);
        }
        string houzui = "";
        if (FileUpload1.FileName.IndexOf(".") > -1)
        {
            houzui = FileUpload1.FileName.Substring(FileUpload1.FileName.IndexOf("."));
        }
        string file= Guid.NewGuid().ToString()+houzui;
        string filePath = path +file;
        FileUpload1.SaveAs(filePath);
        hidImageUrl.Value = file;
        Image1.ImageUrl = "~/file/topics/face/"+file;
        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('上传成功！')", true);
    }
    protected void btnUpload2_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/file/topics/face/");
        if (!System.IO.Directory.Exists(path))
        {
            System.IO.Directory.CreateDirectory(path);
        }
        string houzui = "";
        if (FileUpload2.FileName.IndexOf(".") > -1)
        {
            houzui = FileUpload2.FileName.Substring(FileUpload2.FileName.IndexOf("."));
        }
        string file = Guid.NewGuid().ToString() + houzui;
        string filePath = path + file;
        FileUpload2.SaveAs(filePath);
        hidImageUrl1.Value = file;
        Image2.ImageUrl = "~/file/topics/face/" + file;
        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('上传成功！')", true);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/file/topics/face/");
        if (!System.IO.Directory.Exists(path))
        {
            System.IO.Directory.CreateDirectory(path);
        }
        string houzui = "";
        if (FileUpload3.FileName.IndexOf(".") > -1)
        {
            houzui = FileUpload3.FileName.Substring(FileUpload3.FileName.IndexOf("."));
        }
        string file = Guid.NewGuid().ToString() + houzui;
        string filePath = path + file;
        FileUpload3.SaveAs(filePath);
        hidImageUrl3.Value = file;
        Image3.ImageUrl = "~/file/topics/face/" + file;
        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('上传成功！')", true);
    }
}